package rank

import (
	"time"
)

/**
* Author: dinglin
* Date: 2021/11/2 3:41 下午
 */

/**
* Author: dinglin
* Date: 2021/11/2 3:41 下午
 */

/*
	新创建的优先级最低
		a. pos = 数据库的总条数
		b. suufix = MaxSuffix - curSuffix
		c. count(16) | suffix(48)   max(count) = 65535
	修改的
		a. pos
		b. suffix = MaxSuffix - curSuffix
		c. count(16) | suffix(48)   max(count) = 65535
	新建的情况 pos 严格递增，所以rank严格递增
	修改的情况 pos，suffix 严格递减
	前提是时间是单调的
	//往上拖 3 ----> 1, pos = 1
	//往下拖 1 ----> 3, pos = 4
*/

var (
	//9999-12-31 23:59:59 999 bit(48)
	MaxDate     = time.Date(9999, 12, 31, 23, 59, 59, 0, time.UTC)
	MaxSuffixMs = uint64(MaxDate.Unix()*1000 + 999) //ms
	MaxBit      = 48
)

func MakeRank(pos uint64) uint64 {
	now := time.Now().UTC()
	nowUnixMs := uint64(now.UnixNano() / 1e6)
	if nowUnixMs > MaxSuffixMs {
		panic("time over")
	}
	ret := (pos << MaxBit) | (MaxSuffixMs - nowUnixMs)
	return ret
}
